<?php
/**
 * 银行相关接口，提供给MPE系统调用
 * @author Liuyi
 * @name BankService
 * @Create 2016-11-1 15:16:14
 * @Copyright MPE.Fiidee.Monda.Cn
 */
namespace api\service;

use api\service\interfaces\IBankService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

Loader::import('api.service.interfaces.IBankService');

/**
 * user api(Service)接口实现
 * @author yangjian<yangjian102621@gmail.com>
 */
class BankService implements IBankService {

    /**
     * 加载银行列表，默认返回已启用的银行列表
     * @param $conditions array 查询条件,$conditions=array('status'=>int)
     * @param $fields
     * @param null $order
     * @param int $page
     * @param int $pagesize
     * @return mixed
     */
    public function getBanks( $conditions, $fields, $order=null, $page=1, $pagesize=2147483648) {
        if( !$conditions['status'] ){
            $conditions['status']=1;
        }
        $service = Beans::get('payment.bank.service');
        $banks = $service
            ->where($conditions)
            ->field($fields)
            ->sort($order)
            ->limit($page,$pagesize)
            ->find();
        $count = $service ->count($conditions);
        if( $banks && $count ) {

            $result =  new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", null);
            $result -> putItems($banks);
            $result -> putCount($count);
            return $result;
        }else{
            return new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null );
        }
    }

    /**
     * 加载支行列表,返回查询条件对应的支行列表
     * @param $conditions array 查询条件,$conditions = array('bank_id'=>int,'area_id'=>int,'name'=>String|null)
     * @param $fields string 查询字段
     * @param $order string 排序
     * @param $page int 页码
     * @param $pagesize int 每页数量
     * @return mixed
     */
    public function getBankBranches($conditions, $fields='id,name,number', $order=null, $page=1, $pagesize=2147483648)
    {
        if( !isset($conditions['bank_id']) || !isset($conditions['area_id']) ){
            return new AjaxResult(AjaxResult::OP_FAILURE, 'error params', null);
        }
        $service = Beans::get('payment.bankCode.service');
        $items = $service
            ->where($conditions)
            ->field( $fields )
            ->sort($order)
            ->limit($page,$pagesize)->find();
        $count = $service -> count($conditions);
        if ( $items === false) {
            return new AjaxResult(AjaxResult::OP_FAILURE, '获取数据失败', $items);
        } else {
            $result = new AjaxResult(AjaxResult::OP_SUCCESS, '获取数据成功', null);
            $result -> putItems($items);
            $result -> putCount($count);
            return $result;
        }
    }

    /**
     * 加载支付通道,默认返回已启用的支付通道
     * @param $conditions array 查询条件，$conditions=array('status'=>int)
     * @param $fields string 查询字段
     * @param $order string 排序
     * @param $page int 页码
     * @param $pagesize int 每页数量
     * @return AjaxResult
     */
    public function loadPayways($conditions, $fields="*", $order=null, $page=1, $pagesize=2147483648 ) {
        if( !$conditions['status'] ){
            $conditions['status']=1;
        }
        $service = Beans::get('payment.payway.service');
        $payways = $service
            ->where($conditions)
            ->field($fields)
            ->sort($order)
            ->limit($page,$pagesize)
            ->find();
        $count = $service -> count($conditions);
        if( $payways &&$count ) {
            $result =  new AjaxResult(AjaxResult::OP_SUCCESS, "success", null);
            $result -> putItems($payways);
            $result -> putCount($count);
            return $result;
        }else{
            return new AjaxResult(AjaxResult::OP_FAILURE,"支付通道未启用",null);//load payWay fail
        }
    }

}
